

import plotsvcPkg from './plotservice.js'
var plotsvc = plotsvcPkg.PlotSvc;

import {KeyGenerator}  from '../../../common/keygenerator.js'

import {CGraphTypeUtil} from '../../../graph/paintutil/graphtypeutil.js';
import {LGXEnums}  from '../../../common/sysenum.js'

import snapsvc from '../../../service/pointsnapsvc.js'
var svc = snapsvc.PointSnapSvc;

import utilfuncs from '../../../common/tools/utilfuncs.js';
var funcs = utilfuncs.UtilFuncs;

import globalConst from '../../../common/globalConst.js';
var global = globalConst.GlobalConst;

import { GlobalVar } from '../../../common/globalvar.js';

var LeftMoseDownPlotTool = {

}

LeftMoseDownPlotTool.action = function (dataManger, mosEvnt, mousePt) {
    console.log("lmosdown-1");
    let pCurentGraph = null;
    let scaling = dataManger.getScaling();
    let firstNew = false;
    let toolKind = dataManger.getTool();
    let type = CGraphTypeUtil.getType32(toolKind);
    if (type == LGXEnums.LGXGraphType.TextGraph_type) {
        pCurentGraph = dataManger.getCurentGraph();
        if (pCurentGraph){
            dataManger.setCurentGraph(null);
        }
        return 1;
    }
    if (pCurentGraph){
        dataManger.recordRepaintScope(pCurentGraph);
    }

    if (dataManger.chkCanStartNewGraph()) {
        pCurentGraph = dataManger.productGraph(toolKind);
        if (pCurentGraph) {
            pCurentGraph.setNewAdd(1);
            dataManger.m_topoManager.productNewRes4NewGraph(pCurentGraph);
            pCurentGraph.m_gid = KeyGenerator.gid();
            if (pCurentGraph.m_topoRes) {
                pCurentGraph.m_rid = pCurentGraph.m_topoRes.id;
            }
            firstNew = true;
        } 
    }
    else {
        pCurentGraph = dataManger.getCurentGraph();
    }

    if (firstNew) {
        var devSymbol = dataManger.getSymbol(dataManger.getToolSymbolUUID(), 0);
        if (devSymbol) {
            pCurentGraph.setSymbol(devSymbol);
        }
        dataManger.setCurentGraph(pCurentGraph);
    }
    if (pCurentGraph != null) {
        if (firstNew) {
            dataManger.m_pOperStepService.record(1, []);
        }

        svc.snapMouse2OtherGraphPTFeature(dataManger, mousePt,false,true) ;
        pCurentGraph.act2Mouse(mosEvnt, mousePt);
        pCurentGraph.calScope(1);
        console.log("conn to graph ****************************");
        if (GlobalVar.editMode != "autocad" && funcs.group(toolKind) != global.dimGroup) {
            var mayConnGraph = dataManger.pickGraph(mousePt,"");
            console.log(mayConnGraph);
            if (mayConnGraph && mayConnGraph != pCurentGraph) {
                plotsvc.connect2DstGraph(dataManger.getTopoManager(),mousePt, pCurentGraph, mayConnGraph, firstNew, scaling);
                if (!firstNew){
                    pCurentGraph.setFinish(1);
                    if (pCurentGraph.m_graphType == LGXEnums.LGXGraphType.LineGraph_type){
                        this.correctLineEndPt(pCurentGraph,scaling);
                        let lineDefInfo = pCurentGraph.getLineDef();
                        if (lineDefInfo){
                            lineDefInfo.endArrowType = 'triangle_mid_fill_close';
                        }
                    }
                }
            }
        }

        if (pCurentGraph.getFinish() == 1) {
            dataManger.m_pOperStepService.record(0, []);
        }

        if (pCurentGraph.getStart() == 1 && pCurentGraph.getFinish() == 1) {
            dataManger.getAuxDataManager().hideAuxiliaryGraph();
        }
        if (pCurentGraph){
            dataManger.recordRepaintScope(pCurentGraph);
        }
    }
}

LeftMoseDownPlotTool.correctLineEndPt = function(lineGraph,scaling){
    if (lineGraph.m_graphType != LGXEnums.LGXGraphType.LineGraph_type){
        return 0;
    }
    let position = lineGraph.getPosCopy();
    let ptCount = position.posList.length;
    if (ptCount< 3){
        return 0;
    }
    let tailPt = position.getTail();
    let next2TailPt = position.getNext2Tail();
    let dis = funcs.distancePoint(tailPt,next2TailPt,1);
    const validLen = global.MinSelRectGap/scaling;
    if (dis > validLen){
        return 0;
    }
    //挨着末端点的坐标点如果与末尾基本重合，则从坐标点队列中删掉该点
    position.posList.splice(ptCount-2,1); 
    lineGraph.setPos(position);
    lineGraph.calScope(1);
}


export default { LeftMoseDownPlotTool }